if (files.cwd().endsWith("脚本")) {
  let _require = require;
  require = function (path) {
    if (path.startsWith("./")) {
      path = path.replace("./", "");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}

auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}

// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}

function 广告() {
  let try_count = 10, close_btn = null;
  let progress = 0;
  while (!close_btn) {
    if (try_count <= 0) {
      break
    }
    // 直到找到关闭按钮
    close_btn = id("com.mz.cjjz:id/browser_controller_back").clickable().depth(9).findOne(1000) ||
      id('tt_video_ad_close_layout').clickable().findOne(1000) ||
      className('android.widget.ImageView').clickable().depth(5).drawingOrder(2).findOne(1000) ||
      id("s2s_activity_reward_video_close_imageview").clickable().findOne(1000) ||
      id('com.mz.cjjz:id/iv_ad_down').clickable().findOne(1000) ||
    className('android.view.View').clickable().depth(6).findOne(1000)
    if (close_btn) {
      close_btn.click()
      sleep(time)
    } else {
      try {
        取消 = text("取消").findOne(500)
        if (取消) {
          点击控件(取消)
        }
      } catch (error) { }
      try {
        关闭 = text("关闭").findOne(500)
        if (关闭) {
          点击控件(关闭)
        }
      } catch (error) { }
      try {//支付宝
        if (currentPackage() != 'com.mz.cjjz') {
          toastLog('估计是进入支付宝页面了')
          sleep(2000)
          back()
          sleep(2000)
          继续 = textStartsWith("继续").findOne(1000)
          if (继续) {
            点击控件(继续)
          }
          var 每日任务 = text('每日任务').findOne(3000)
          if (每日任务) {
            break
          }
        }
      } catch (error) { }
    }
    try_count--
    progress++;
    if(progress*10 < 100){
      utils.updateAppProgress(progress*10);
    }    
    sleep(1000)
  }
}

function 去签到() {
  var 去签到 = text("去签到").findOne(5000)
  if (去签到) {
    去签到.click()
    sleep(time)
  }
}

function login() {
  var thread_2 = threads.start(function () {
    toastLog("检查是否登录")
    while (true) {
      click(520,320) ; //点击一次空白地方
      var 登录 = text('登录').findOne(500)
      if (登录) {
        sleep(time)
        登录.click()
        sleep(time)
        break;
      }else{
        back(); //不是登录页面
      }
    }
  })
  var i = 0
  while (i < 20) {
    var 我的 = text("我的").findOne(2000)
    if (我的) {
      break
    }
    i++
  }
  thread_2.interrupt()

}

function task() {
  var 我的 = text("我的").findOne(5000)
  if (我的) {
    我的.parent().click()
    sleep(6000)
    var 每日签到 = text("每日签到").findOne(5000)
    if (每日签到) {
      每日签到.click()
      sleep(time)
    }
    var 已签到 = text("已 签 到").findOne(5000)
    if (已签到) {
      toastLog('已经完成任务')
      sleep(time)
      return ;
    } 
    var arr = text('去完成').find()
    if (arr.length > 0) {
      arr.forEach(target => {
        target.click();
        sleep(5000)
        var 每日任务 = text('每日任务').findOne(1000)
        if (!每日任务) {
          toastLog('当前是广告界面')
          广告()
          去签到()
          sleep(3000)
        }
      });
      var 立即签到 = text("立 即 签 到").findOne(5000)
      if (立即签到) {
        立即签到.click()
        sleep(time)
      }
      var 确定 = text("确定").findOne(5000)
      if (确定) {
        确定.click()
      }
    }
  }
}

function main() {
  let open_app = launchAppEnhance("众赢时空");
  if (open_app) {
    var thread_1 = threads.start(function () {
      toastLog("检查广告弹窗")
      while (true) {
        var widget = id("com.mz.cjjz:id/bt_confirm").findOne(2000)
        if (widget) {
          widget.click()
        }
      }
    })
    login()
    task()
    thread_1.interrupt()
  }
  else {
    toastLog("众赢时空打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}


storage.put("script_running_status", "end");
